package isi_graf;

import java.util.Date;
import java.util.Iterator;

import isi_ib.barra_trade;
import isi_ib.barras_trade;
import isi_ib.isi_estudios;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class isiLineChart {

	private XYDataset dataset;
	private JFreeChart chart;
	private ChartPanel chartPanel;
	String titulo;
	
	
	public  isiLineChart(String titulo_) {
		super();
		dataset=null;
		chart=null;
		chartPanel=null;
		titulo=titulo_;
	}
	
	public ChartPanel CreaChart(barras_trade barras_){
		dataset = createDataset(barras_);
		chart = createChart(dataset);
		Ajusta(barras_.minvalue,barras_.maxvalue);
		chartPanel = new ChartPanel(chart);
		return chartPanel;
	}
	
	private void Ajusta(Double minvalue_,Double maxvalue_){
		if(chart==null) return;
	/*	if(minvalue_==0 && maxvalue_==0){
			minvalue_ = getLowestLow(dataset);
			maxvalue_ = getHighestHigh(dataset);
		}*/
		chart.getXYPlot().getRangeAxis().setRange(minvalue_,maxvalue_);
	}
		
	
	
	private JFreeChart createChart(XYDataset dataset_) {
			  if(dataset==null) return null; 
			  JFreeChart chart = ChartFactory.createXYLineChart(
			            titulo,      // chart title
			            "X",                      // x axis label
			            "Y",                      // y axis label
			            dataset,                  // data
			            PlotOrientation.VERTICAL,
			            true,                     // include legend
			            true,                     // tooltips
			            false                     // urls
			        );
			  return chart;
			  }
	private XYDataset createDataset(barras_trade barras_) {

		

     
        
		XYSeriesCollection dataset = new XYSeriesCollection();	 
		XYSeries series1 = new XYSeries("6");
		XYSeries series2 = new XYSeries("3");
		isi_estudios estudios = new isi_estudios(barras_,null,"",false);
		
		
			  Iterator<barra_trade> itr = barras_.barras.iterator();
			  int i=0;
			    while (itr.hasNext()) {
			    	barra_trade element=itr.next();
			    	int aaa_=barras_.barras.indexOf(element);
			    	//series1.add(i, estudios.Media_movil_exponencial(element,3,"oscK"));
			    	//series2.add(i, estudios.Media_simple(element,3,"oscK"));
			    	
			    	//series1.add(i, estudios.Media_movil_exponencial(element,6,"close"));
			    	//series2.add(i, estudios.Media_simple(element,3,"close"));
			      i++;
			    }
			    dataset.addSeries(series1);		
			    dataset.addSeries(series2);	
			  return dataset;
			  }
	
}
